﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service;
using OneMG.Phoenix.Kernel.Enum;


namespace OneMG.Phoenix.AdminSite.Views.Product
{
    public partial class Edit : System.Web.UI.Page
    {
        ICategoryService ICategory = new CategoryService();

        IProductService IProduct = new ProductService();

        IProductPhotosService IProductPhotos = new ProductPhotosService();

        public IList<CategoryModel> CategoryList = new List<CategoryModel>();

        public ProductModel objProduct;

        public string ProductPhotoUrls = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CategoryList = ICategory.GetByParentID(2);

                if (!string.IsNullOrEmpty(Request.QueryString["pid"]))
                {
                    int productID = int.Parse(Request.QueryString["pid"]);

                    objProduct = IProduct.GetByProductID(productID);

                    GetPhotos(productID);
                }

            }
        }

        private void GetPhotos(int productID)
        {
            IList<ProductPhotosModel> listModel = new List<ProductPhotosModel>();
            listModel = IProductPhotos.GetListByProductID(productID);

            foreach (var item in listModel)
            {
                ProductPhotoUrls += item.Photo + "|";
            }


        }

        private void AddProduct()
        {

            ProductModel productModel = new ProductModel()
            {
                CateID = int.Parse(Request.Form["sltCategoryID"]),
                AssistantPhotographer = Request.Form["txtAssistantPhotographer"],
                Model = Request.Form["txtModel"],
                Photo = "aa",//Request.Form["txtPhoto"],
                Photographer = Request.Form["txtPhotographer"],
                Stylist = Request.Form["txtStylist"],
                Title = Request.Form["txtTitle"],
                Description = Request.Form["txtDescription"],
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Parse("1900-01-01"),
                UpdateUser = 0,
                CreateUser = 0,
                Visitation = 0
            };

            int productID = IProduct.AddProduct(productModel);


            string photos = Request.Form["hdPhotos"];

            if (!string.IsNullOrEmpty(photos))
            {
                string[] urls = photos.Split('|');
                foreach (var item in urls)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        ProductPhotosModel photosModel = new ProductPhotosModel()
                        {
                            CreateTime = DateTime.Now,
                            Photo = item,
                            ProductID = productID
                        };

                        IProductPhotos.AddProductPhotos(photosModel);

                    }

                }
            }




        }

        private void UpdateProduct()
        {
            int productID = int.Parse(Request.Form["hdpid"]);

            ProductModel productModel = IProduct.GetByProductID(productID);
            productModel.CateID = int.Parse(Request.Form["sltCategoryID"]);
            productModel.AssistantPhotographer = Request.Form["txtAssistantPhotographer"];
            productModel.Model = Request.Form["txtModel"];
            productModel.Photo = "aa";//Request.Form["txtPhoto"],
            productModel.Photographer = Request.Form["txtPhotographer"];
            productModel.Stylist = Request.Form["txtStylist"];
            productModel.Title = Request.Form["txtTitle"];
            productModel.Description = Request.Form["txtDescription"];
            productModel.UpdateTime = DateTime.Now;
            productModel.UpdateUser = 0;

            //更新主信息
            IProduct.UpdateByProductID(productModel);

            //删除图片数据

            IProductPhotos.GetListByProductID(productID);

            //添加图片数据
            string photos = Request.Form["hdPhotos"];

            if (!string.IsNullOrEmpty(photos))
            {
                string[] urls = photos.Split('|');
                foreach (var item in urls)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        ProductPhotosModel photosModel = new ProductPhotosModel()
                        {
                            CreateTime = DateTime.Now,
                            Photo = item,
                            ProductID = productID
                        };

                        IProductPhotos.AddProductPhotos(photosModel);

                    }

                }
            }


        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            int actionType = 0;
            int.TryParse(Request.Form["hdaction"], out actionType);

            switch ((ActionType)actionType)
            {
                case ActionType.Insert:
                    {
                        AddProduct();

                    }
                    break;
                case ActionType.Modify:
                    {
                        UpdateProduct();
                    }
                    break;

            }
        }
    }
}
